/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrameProxy.java
 *
 * Created on 16/07/2012, 14:28:22
 */
package epibot.robot.Proxy;

import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Rafael Tosta
 */
public class FrameProxy extends javax.swing.JDialog {

    private String proxyServer;
    private String portServer;
    private boolean ativa;

    /** Creates new form FrameProxy */
    public FrameProxy(Frame f) {
        super(f, true);
        setName("Frame Proxy");
        Image imagemTitulo = Toolkit.getDefaultToolkit().getImage(this.getClass().getResource("/epibot/GUI/appICO.png"));
        this.setIconImage(imagemTitulo);
        initComponents();

        setLocationRelativeTo(null);
        lerArquivo();
    }

    private String abreArquivo(File arquivo) throws FileNotFoundException, IOException {
        int size = (int) arquivo.length();
        byte[] buffer = new byte[size];
        InputStream in = new FileInputStream(arquivo);
        in.read(buffer);
        String stringEntrada = new String(buffer).trim().toString();
        in.close();

        return stringEntrada;
    }

    private void lerArquivo() {
        File arquivo;

        arquivo = new File("Proxy.tos");

        if (arquivo.exists()) {

            try {
                String texto = this.abreArquivo(arquivo);
                StringTokenizer p1 = new StringTokenizer(texto, ",");
                this.proxyServer = p1.nextToken();
                this.portServer = p1.nextToken();

                this.proxy.setText(proxyServer);
                this.port.setText(portServer);

            } catch (Exception ex) {
                Logger.getLogger(FrameProxy.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    public void gravaArquivo(String texto) {

        try {
            BufferedWriter saida = new BufferedWriter(new FileWriter("Proxy.tos"));

            saida.write(texto);
            saida.newLine();

            saida.close();

            //JOptionPane.showMessageDialog(this, "Configured!");
        } // em caso de erro apreenta mensagem abaixo
        catch (Exception e) {
            //   JOptionPane.showMessageDialog(this, "Atenção");
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jCheckBox1 = new javax.swing.JCheckBox();
        proxy = new javax.swing.JTextField();
        port = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButtonOk = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("EPIBOT Proxy Settings");
        setResizable(false);

        jPanel1.setMaximumSize(new java.awt.Dimension(900, 600));
        jPanel1.setPreferredSize(new java.awt.Dimension(556, 370));

        jCheckBox1.setText("Use Proxy Server");
        jCheckBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCheckBox1ItemStateChanged(evt);
            }
        });

        proxy.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                proxyMouseClicked(evt);
            }
        });

        port.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                portMouseClicked(evt);
            }
        });

        jLabel1.setText("Port");

        jLabel2.setText("Proxy Settings");

        jButtonOk.setText("Apply");
        jButtonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOkActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addComponent(jCheckBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(46, 46, 46)
                        .addComponent(proxy, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(port, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addComponent(jButtonOk, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(36, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jCheckBox1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(jLabel1)))
                .addGap(2, 2, 2)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(proxy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addComponent(jButtonOk)
                .addContainerGap(41, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 380, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jCheckBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCheckBox1ItemStateChanged
    if (ativa == true) {
        ativa = false;
    } else {
        ativa = true;
    }
    this.port.setEditable(ativa);
    this.proxy.setEditable(ativa);
}//GEN-LAST:event_jCheckBox1ItemStateChanged

private void proxyMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_proxyMouseClicked
    this.jButtonOk.setEnabled(true);
}//GEN-LAST:event_proxyMouseClicked

private void portMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_portMouseClicked
    this.jButtonOk.setEnabled(true);
}//GEN-LAST:event_portMouseClicked

private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOkActionPerformed
    if (ativa == true) {

        this.proxyServer = this.proxy.getText();
        this.portServer = this.port.getText();
        if (!this.proxyServer.equals("") && !this.portServer.equals("")) {
            this.gravaArquivo(proxyServer + "," + portServer);
            GereciadorDoProxy.setAtiva(true, proxyServer, Integer.parseInt(portServer));
            this.jButtonOk.setEnabled(false);
        } else {
            JOptionPane.showMessageDialog(this, "Insert proxy!");
        }
    } else {
        GereciadorDoProxy.setAtiva(false);
        this.proxyServer = null;
        this.portServer = null;
    }
    dispose();
    // System.out.println(this.proxyServer + " " + this.portServer);
}//GEN-LAST:event_jButtonOkActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonOk;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField port;
    private javax.swing.JTextField proxy;
    // End of variables declaration//GEN-END:variables
}
